package medium;

import java.util.*;
import java.util.concurrent.locks.Condition;

/*
151. 翻转字符串里的单词
解题思路：
1.调用内置api
2.手写实现方法
 */
public class LeeCode151 {

  public static void main(String[] args) {
    System.out.println(new LeeCode151().reverseWords("the sky is blue"));
  }

  public String reverseWords(String s) {
//    int left = 0;
//    int right = s.length() - 1;
//    while (left <= right && s.charAt(left) == ' '){
//      left++;
//    }
//    while (left <= right && s.charAt(right) == ' '){
//      right--;
//    }
//    StringBuilder builder = new StringBuilder();
//    Deque<String> deque = new ArrayDeque<String>();
//    while (left <= right){
//      char c = s.charAt(left);
//      if (builder.length() != 0 && c == ' '){
//        deque.offerFirst(builder.toString());
//        builder.setLength(0);
//      }else if (c != ' '){
//        builder.append(c);
//      }
//      left++;
//    }
//    deque.offerFirst(builder.toString());
//    return String.join(" ",deque);
    String trim = s.trim();
    List<String> list = Arrays.asList(trim.split("\\s+"));
    Collections.reverse(list);
    return String.join(" ",list);
  }

}
